﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using Moq;
using Newtonsoft.Json;
using Sustainalytics.ClientDW.DataAccessLayer.ESGRatings;
using Sustainalytics.ClientDW.Entities;
using Sustainalytics.DataLayer.CompanyHistory;
using Sustainalytics.DataLayer.Shared;
using Sustainalytics.Entities;
using Sustainalytics.Entities.ESGRatings;
using Xunit;
using Sustainalytics.BusinessResearchProductStates.Entities.ResearchState;

namespace Sustainalytics.HistoricalData.ComputationTool.Tests
{

    public class HistoricalComputationToolTests
    {
        private readonly string _clientConnectionString = "mongodb://localhost:27017/ClientDW_B";
        private readonly Guid _companyId = new Guid("904c488c-d619-40d4-90b8-2d3bce8161eb");//tata corporation
        private readonly string _sutConnectionString = "mongodb://localhost:27017/DWArchive_B";
        private readonly Guid _companyId2 = new Guid("9d033c46-633e-46c5-893d-f3efcdb09ad1"); //microsoft corporation
        private readonly Guid _companyId3 = new Guid("d0b34a90-9d4a-42cf-89c8-5c01258d7776"); //wipro
        private readonly Guid _companyId4 = new Guid("E1B47E9E-051A-48CE-ADDF-74E8500EA7FD"); //Banco comercial....
        public readonly Guid _companyId5 = new Guid("dba613cf-47fc-4e4d-a149-47da9bc13185"); //DNB ASA
        private HistoricalDataSyncTool _sutHistoricalDataSyncTool;

        [Fact]
        public void IndicatorsExclusionPolicy_All_Types_Indicators_All_E_GreaterThanThreshold_Test()
        {
            CompanyHistoricalDataIndicators indicators = new CompanyHistoricalDataIndicators
            {
                CapitalIQID = string.Empty,
                CompanyId = new Guid(),
                Id = new Guid(),
                Indicators = new List<IndicatorData>
                {
                    new IndicatorData
                    {
                        IndicatorNumber = "E.1.1",
                        RawScore = 100,
                        Weight = 20
                    },
                     new IndicatorData
                    {
                        IndicatorNumber = "E.1.2",
                        RawScore = 100,
                        Weight = 20
                    },
                    new IndicatorData
                    {
                        IndicatorNumber = "S.1.2",
                        RawScore = 100,
                        Weight = 20
                    }, new IndicatorData
                    {
                        IndicatorNumber = "G.1.3",
                        RawScore =55,
                        Weight = 20
                    },

                }
            };


            var sut = HistoricalDataSyncTool.CompanyExclusionPolicy(indicators);
            Assert.False(sut);


        }

        [Fact]
        public void IndicatorsExclusionPolicy_All_Types_Indicators_Of_Type_P_Should_ExcludeThem()
        {
            CompanyHistoricalDataIndicators indicators = new CompanyHistoricalDataIndicators
            {
                CapitalIQID = string.Empty,
                CompanyId = new Guid(),
                Id = new Guid(),
                Indicators = new List<IndicatorData>
                {
                    new IndicatorData
                    {
                        IndicatorNumber = "E.1.1",
                        RawScore = 0,
                        Weight = 20
                    },
                     new IndicatorData
                    {
                        IndicatorNumber = "E.1.2",
                        RawScore = 100,
                        Weight = 20
                    },
                    new IndicatorData
                    {
                        IndicatorNumber = "S.1.2",
                        RawScore = 75,
                        Weight = 20
                    },
                    new IndicatorData
                    {
                        IndicatorNumber = "G.1.3",
                        RawScore =55,
                        Weight = 20
                    },
                    new IndicatorData
                    {
                        IndicatorNumber = "P.1.3",
                        RawScore =100,
                        Weight = 20
                    },

                }
            };


            var sut = HistoricalDataSyncTool.CompanyExclusionPolicy(indicators);
            Assert.True(sut);


        }

        [Fact]
        public void IndicatorsExclusionPolicy_Only_E_Indicators_All_E_GreaterThanThreshold_Test()
        {
            CompanyHistoricalDataIndicators indicators = new CompanyHistoricalDataIndicators
            {
                CapitalIQID = string.Empty,
                CompanyId = new Guid(),
                Id = new Guid(),
                Indicators = new List<IndicatorData>
                {
                    new IndicatorData
                    {
                        IndicatorNumber = "E.1.1",
                        RawScore = 100,
                        Weight = 20
                    },
                    new IndicatorData
                    {
                        IndicatorNumber = "E.1.2",
                        RawScore = 100,
                        Weight = 20
                    }
                }
            };




            var sut = HistoricalDataSyncTool.CompanyExclusionPolicy(indicators);
            Assert.False(sut);


        }

        [Fact]
        public void IndicatorsExclusionPolicy_Only_E_Indicators_Some_E_LowerThanThreshold_Test()
        {
            CompanyHistoricalDataIndicators indicators = new CompanyHistoricalDataIndicators
            {
                CapitalIQID = string.Empty,
                CompanyId = new Guid(),
                Id = new Guid(),
                Indicators = new List<IndicatorData>
                {
                    new IndicatorData
                    {
                        IndicatorNumber = "E.1.1",
                        RawScore = 100,
                        Weight = 20
                    },
                    new IndicatorData
                    {
                        IndicatorNumber = "E.1.2",
                        RawScore = 100,
                        Weight = 20
                    }, new IndicatorData
                    {
                        IndicatorNumber = "E.1.3",
                        RawScore =55,
                        Weight = 20
                    },

                }
            };


            var sut = HistoricalDataSyncTool.CompanyExclusionPolicy(indicators);
            Assert.True(sut);


        }


        [Fact]
        public void IndicatorsExclusionPolicy_All_Types_Indicators_Some_E_LoweThanThreshold_And_Some_E_Null_Indicators_Test()
        {
            CompanyHistoricalDataIndicators indicators = new CompanyHistoricalDataIndicators
            {
                CapitalIQID = string.Empty,
                CompanyId = new Guid(),
                Id = new Guid(),
                Indicators = new List<IndicatorData>
                {
                    new IndicatorData
                    {
                        IndicatorNumber = "E.1.1",
                        RawScore = 100,
                        Weight = 20
                    },
                    new IndicatorData
                    {
                        IndicatorNumber = "E.1.1",
                        RawScore = 85,
                        Weight = 20
                    },

                    new IndicatorData
                    {
                        IndicatorNumber = "S.1.2",
                        RawScore = 25,
                        Weight = 20
                    }, new IndicatorData
                    {
                        IndicatorNumber = "G.1.3",
                        RawScore =55,
                        Weight = 20
                    }, new IndicatorData
                    {
                        IndicatorNumber = "E.1.3",
                        RawScore =null,
                        Weight = null
                    }, new IndicatorData
                    {
                        IndicatorNumber = "G.1.2",
                        RawScore =55,
                        Weight = 20
                    },

                }
            };


            var sut = HistoricalDataSyncTool.CompanyExclusionPolicy(indicators);
            Assert.True(sut);


        }


        [Fact]
        public void FilteringIndicatorsPolicy_Other_Indicator_Types_Some_E_GreaterThanThreshold_And_SomeNull_Indicators_Test()
        {
            CompanyHistoricalDataIndicators indicators = new CompanyHistoricalDataIndicators
            {
                CapitalIQID = string.Empty,
                CompanyId = Guid.NewGuid(),
                Id = Guid.NewGuid(),
                Indicators = new List<IndicatorData>
                {
                    new IndicatorData
                    {
                        IndicatorNumber = "E.1.1",
                        RawScore = 100,
                        Weight = 20
                    },
                    new IndicatorData
                    {
                        IndicatorNumber = "S.1.2",
                        RawScore = 25,
                        Weight = 20
                    }, new IndicatorData
                    {
                        IndicatorNumber = "G.1.3",
                        RawScore =55,
                        Weight = 20
                    }, new IndicatorData
                    {
                        IndicatorNumber = "E.1.3",
                        RawScore =null,
                        Weight = null
                    }, new IndicatorData
                    {
                        IndicatorNumber = "P.1.2",
                        RawScore =55,
                        Weight = 20
                    },

                }
            };


            var sut = HistoricalDataSyncTool.FilterIndicatorsByThemeAndNotNullValue(new[] { indicators });
            Assert.True(sut.Count() == 1);
            Assert.Equal(sut.FirstOrDefault().Indicators.Count(), 3);

        }


        [Fact]
        public void TestMethod_WithOne_OutOfRangeIndicators_Company()
        {
            var testData = GetCorporateData(new[] { _companyId4 });
            var corporateDataMock = new Mock<IRepositoryCollection<CorporateData>>();
            corporateDataMock.Setup(m => m.ReadWhere(x => x.ComprehensiveEsgResearchState.ResearchState == ResearchState.ResearchAvailable, null, null, null, x => x.Id, x => x.ComprehensiveEsgResearchState, x => x.ControversyResearchState, x => x.CapitalIqId, x => x.CompanyName, x => x.CompanyType, x => x.GICSIndustryGroup, x => x.PeerGroupShortName, x => x.TemplateType, x => x.UniverseIds)).Returns(testData);

            IRepositoryCollection<CompanyHistoricalDataIndicators> historicalDataRepository = new CompanyHistoricalDataIndicatorsRepository(_sutConnectionString, "CompanyHistoricalDataIndicators_copy");
            IReferenceUniversesRepository universeRepository = ReferenceUniversesRepository.GetInstance(_clientConnectionString);
            IWeightMatricesRepository weightMatrixRepository = WeightMatricesRepository.GetInstance(_clientConnectionString);
            IRepositoryCollection<ESGProfile> esgProfileRepository = new RepositoryCollection<ESGProfile>(_clientConnectionString);
            IRepositoryCollection<CompanyHistoricalPerformance> companyHistoricalPerformanceLast36MonthsRepository = new RepositoryCollection<CompanyHistoricalPerformance>(_sutConnectionString);
            IRepositoryCollection<IndicatorTemplate> indicatorTemplateRepository = new RepositoryCollection<IndicatorTemplate>(_clientConnectionString);
            _sutHistoricalDataSyncTool = new HistoricalDataSyncTool(corporateDataMock.Object, historicalDataRepository, universeRepository, weightMatrixRepository, esgProfileRepository, companyHistoricalPerformanceLast36MonthsRepository, indicatorTemplateRepository);
            var data = _sutHistoricalDataSyncTool.ComputeHistoryData(new DateTime(2015, 08, 01));

            var text = JsonConvert.SerializeObject(data, Formatting.Indented);

            File.WriteAllText(@"c:\programdata\testResult.txt", text);

            Assert.False(data.Any());
        }

        [Fact]
        public void TestMethod_WithOne_InRangeIndicators_Company()
        {
            var testData = GetCorporateData(new[] { _companyId });
            var corporateDataMock = new Mock<IRepositoryCollection<CorporateData>>();
            corporateDataMock.Setup(m => m.ReadWhere(x => x.ComprehensiveEsgResearchState.ResearchState == ResearchState.ResearchAvailable, null, null, null, x => x.Id, x => x.ComprehensiveEsgResearchState, x => x.ControversyResearchState, x => x.CapitalIqId, x => x.CompanyName, x => x.CompanyType, x => x.GICSIndustryGroup, x => x.PeerGroupShortName, x => x.TemplateType, x => x.UniverseIds)).Returns(testData);

            IRepositoryCollection<CompanyHistoricalDataIndicators> historicalDataRepository = new CompanyHistoricalDataIndicatorsRepository(_sutConnectionString);
            IReferenceUniversesRepository universeRepository = ReferenceUniversesRepository.GetInstance(_clientConnectionString);
            IWeightMatricesRepository weightMatrixRepository = WeightMatricesRepository.GetInstance(_clientConnectionString);
            IRepositoryCollection<ESGProfile> esgProfileRepository = new RepositoryCollection<ESGProfile>(_clientConnectionString);
            IRepositoryCollection<CompanyHistoricalPerformance> companyHistoricalPerformanceLast36MonthsRepository = new RepositoryCollection<CompanyHistoricalPerformance>(_sutConnectionString);
            IRepositoryCollection<IndicatorTemplate> indicatorTemplateRepository = new RepositoryCollection<IndicatorTemplate>(_clientConnectionString);
            _sutHistoricalDataSyncTool = new HistoricalDataSyncTool(corporateDataMock.Object, historicalDataRepository, universeRepository, weightMatrixRepository, esgProfileRepository, companyHistoricalPerformanceLast36MonthsRepository, indicatorTemplateRepository);
            var data = _sutHistoricalDataSyncTool.ComputeHistoryData(new DateTime(2014, 05, 02), 1);

            var text = JsonConvert.SerializeObject(data, Formatting.Indented);

            File.WriteAllText(@"c:\programdata\testResult.txt", text);

            Assert.True(data.Any());
        }


        [Fact]
        public void TestMethod_With_Six_Companies_One_OutOfRangeIndicators_Company()
        {
            var corporateTestData = GetCorporateData(new[] { _companyId, _companyId2, new Guid("18a047b2-11a7-435a-9842-c419430aeb32"), new Guid("8763854e-da7e-4dfc-9353-92b8ed69b0e2"), new Guid("1d924fa2-af88-4f37-bf34-4da69005531e"), new Guid("d0b34a90-9d4a-42cf-89c8-5c01258d7776") });

            var testDate = new DateTime(2014, 05, 02);

            var corporateDataMock = new Mock<IRepositoryCollection<CorporateData>>();
            corporateDataMock.Setup(m => m.ReadWhere(x => x.ComprehensiveEsgResearchState.ResearchState == ResearchState.ResearchAvailable, null, null, null, x => x.Id, x => x.ComprehensiveEsgResearchState, x => x.ControversyResearchState, x => x.CapitalIqId, x => x.CompanyName, x => x.CompanyType, x => x.GICSIndustryGroup, x => x.PeerGroupShortName, x => x.TemplateType, x => x.UniverseIds)).Returns(corporateTestData);

            IRepositoryCollection<CompanyHistoricalDataIndicators> historicalDataRepository = new CompanyHistoricalDataIndicatorsRepository(_sutConnectionString, "CompanyHistoricalDataIndicators_copy");
            IReferenceUniversesRepository universeRepository = ReferenceUniversesRepository.GetInstance(_clientConnectionString);
            IWeightMatricesRepository weightMatrixRepository = WeightMatricesRepository.GetInstance(_clientConnectionString);
            IRepositoryCollection<ESGProfile> esgProfileRepository = new RepositoryCollection<ESGProfile>(_clientConnectionString);
            IRepositoryCollection<CompanyHistoricalPerformance> companyHistoricalPerformanceLast36MonthsRepository = new RepositoryCollection<CompanyHistoricalPerformance>(_sutConnectionString);
            IRepositoryCollection<IndicatorTemplate> indicatorTemplateRepository = new RepositoryCollection<IndicatorTemplate>(_clientConnectionString);
            _sutHistoricalDataSyncTool = new HistoricalDataSyncTool(corporateDataMock.Object, historicalDataRepository, universeRepository, weightMatrixRepository, esgProfileRepository, companyHistoricalPerformanceLast36MonthsRepository, indicatorTemplateRepository);
            var data = _sutHistoricalDataSyncTool.ComputeHistoryData(testDate, 1);

            var text = JsonConvert.SerializeObject(data, Formatting.Indented);

            File.WriteAllText(@"c:\programdata\testResult.txt", text);

            Assert.Equal(6, data.Count());
        }


        [Fact]
        public void TestMethod_For_CompanyWithBump_Company()
        {
            var corporateTestData = GetCorporateData(new[] { _companyId5 });

            var testDate = new DateTime(2014, 06, 01);

            var corporateDataMock = new Mock<IRepositoryCollection<CorporateData>>();
            corporateDataMock.Setup(m => m.ReadWhere(x => x.ComprehensiveEsgResearchState.ResearchState == ResearchState.ResearchAvailable, null, null, null, x => x.Id, x => x.ComprehensiveEsgResearchState, x => x.ControversyResearchState, x => x.CapitalIqId, x => x.CompanyName, x => x.CompanyType, x => x.GICSIndustryGroup, x => x.PeerGroupShortName, x => x.TemplateType, x => x.UniverseIds)).Returns(corporateTestData);

            IRepositoryCollection<CompanyHistoricalDataIndicators> historicalDataRepository = new CompanyHistoricalDataIndicatorsRepository(_sutConnectionString);
            IReferenceUniversesRepository universeRepository = ReferenceUniversesRepository.GetInstance(_clientConnectionString);
            IWeightMatricesRepository weightMatrixRepository = WeightMatricesRepository.GetInstance(_clientConnectionString);
            IRepositoryCollection<ESGProfile> esgProfileRepository = new RepositoryCollection<ESGProfile>(_clientConnectionString);
            IRepositoryCollection<CompanyHistoricalPerformance> companyHistoricalPerformanceLast36MonthsRepository = new RepositoryCollection<CompanyHistoricalPerformance>(_sutConnectionString);
            IRepositoryCollection<IndicatorTemplate> indicatorTemplateRepository = new RepositoryCollection<IndicatorTemplate>(_clientConnectionString);
            _sutHistoricalDataSyncTool = new HistoricalDataSyncTool(corporateDataMock.Object, historicalDataRepository, universeRepository, weightMatrixRepository, esgProfileRepository, companyHistoricalPerformanceLast36MonthsRepository, indicatorTemplateRepository);
            var data = _sutHistoricalDataSyncTool.ComputeHistoryData(testDate, 1);

            var text = JsonConvert.SerializeObject(data, Formatting.Indented);

            File.WriteAllText(@"c:\programdata\testResult.txt", text);

            Assert.True(data.Any());
        }


        [Fact]
        public void Timing_Filtering_TestMethods()
        {
            var testData = GetCorporateData(new[] { _companyId });
            var corporateDataMock = new Mock<IRepositoryCollection<CorporateData>>();
            corporateDataMock.Setup(m => m.ReadWhere(x => x.ComprehensiveEsgResearchState.ResearchState == ResearchState.ResearchAvailable, null, null, null, x => x.Id, x => x.ComprehensiveEsgResearchState, x => x.ControversyResearchState, x => x.CapitalIqId, x => x.CompanyName, x => x.CompanyType, x => x.GICSIndustryGroup, x => x.PeerGroupShortName, x => x.TemplateType, x => x.UniverseIds)).Returns(testData);

            IRepositoryCollection<CompanyHistoricalDataIndicators> historicalDataRepository = new CompanyHistoricalDataIndicatorsRepository(_sutConnectionString);
            IReferenceUniversesRepository universeRepository = ReferenceUniversesRepository.GetInstance(_clientConnectionString);
            IWeightMatricesRepository weightMatrixRepository = WeightMatricesRepository.GetInstance(_clientConnectionString);
            IRepositoryCollection<ESGProfile> esgProfileRepository = new RepositoryCollection<ESGProfile>(_clientConnectionString);
            IRepositoryCollection<CompanyHistoricalPerformance> companyHistoricalPerformanceLast36MonthsRepository = new RepositoryCollection<CompanyHistoricalPerformance>(_sutConnectionString);
            IRepositoryCollection<IndicatorTemplate> indicatorTemplateRepository = new RepositoryCollection<IndicatorTemplate>(_clientConnectionString);
            _sutHistoricalDataSyncTool = new HistoricalDataSyncTool(corporateDataMock.Object, historicalDataRepository, universeRepository, weightMatrixRepository, esgProfileRepository, companyHistoricalPerformanceLast36MonthsRepository, indicatorTemplateRepository);


            var watch = new Stopwatch();

            watch.Start();
            var data = _sutHistoricalDataSyncTool.ComputeHistoryData(new DateTime(2014, 05, 02), 1);
            watch.Stop();

            Console.WriteLine(" Time Elapsed {0} ms", watch.Elapsed.TotalMilliseconds);


            watch.Start();

            IRepositoryCollection<CompanyHistoricalDataIndicators> historicalDataRepository2 = new RepositoryCollection<CompanyHistoricalDataIndicators>(_sutConnectionString);

            var sutHistoricalDataSyncTool2 = new HistoricalDataSyncTool(corporateDataMock.Object, historicalDataRepository2, universeRepository, weightMatrixRepository, esgProfileRepository, companyHistoricalPerformanceLast36MonthsRepository, indicatorTemplateRepository);
            var data2 = sutHistoricalDataSyncTool2.ComputeHistoryData(new DateTime(2014, 05, 02), 1);

            watch.Stop();

            Console.WriteLine(" Time Elapsed {0} ms", watch.Elapsed.TotalMilliseconds);


            Assert.True(data.Any());
        }



        [Fact]
        public void Second_Timing_Filtering_TestMethods()
        {
            var testData = GetCorporateData(new[] { _companyId });
            var corporateDataMock = new Mock<IRepositoryCollection<CorporateData>>();
            corporateDataMock.Setup(m => m.ReadWhere(x => x.ComprehensiveEsgResearchState.ResearchState == ResearchState.ResearchAvailable, null, null, null, x => x.Id, x => x.ComprehensiveEsgResearchState, x => x.ControversyResearchState, x => x.CapitalIqId, x => x.CompanyName, x => x.CompanyType, x => x.GICSIndustryGroup, x => x.PeerGroupShortName, x => x.TemplateType, x => x.UniverseIds)).Returns(testData);

            IRepositoryCollection<CompanyHistoricalDataIndicators> historicalDataRepository = new CompanyHistoricalDataIndicatorsRepository(_sutConnectionString);
            IReferenceUniversesRepository universeRepository = ReferenceUniversesRepository.GetInstance(_clientConnectionString);
            IWeightMatricesRepository weightMatrixRepository = WeightMatricesRepository.GetInstance(_clientConnectionString);
            IRepositoryCollection<ESGProfile> esgProfileRepository = new RepositoryCollection<ESGProfile>(_clientConnectionString);
            IRepositoryCollection<CompanyHistoricalPerformance> companyHistoricalPerformanceLast36MonthsRepository = new RepositoryCollection<CompanyHistoricalPerformance>(_sutConnectionString);
            IRepositoryCollection<IndicatorTemplate> indicatorTemplateRepository = new RepositoryCollection<IndicatorTemplate>(_clientConnectionString);
            _sutHistoricalDataSyncTool = new HistoricalDataSyncTool(corporateDataMock.Object, historicalDataRepository, universeRepository, weightMatrixRepository, esgProfileRepository, companyHistoricalPerformanceLast36MonthsRepository, indicatorTemplateRepository);


            var watch = new Stopwatch();

            watch.Start();
            var data = _sutHistoricalDataSyncTool.ComputeHistoryData(new DateTime(2014, 05, 02), 1);
            watch.Stop();

            Console.WriteLine(" Time Elapsed {0} ms", watch.Elapsed.TotalMilliseconds);



            Assert.True(data.Any());
        }


        [Fact]
        public void Third_Timing_Filtering_TestMethods()
        {
            var corporateTestData =
                GetCorporateData(new[]
                {
                    _companyId, _companyId2
                    , new Guid("18a047b2-11a7-435a-9842-c419430aeb32")
                    , new Guid("8763854e-da7e-4dfc-9353-92b8ed69b0e2")
                    , new Guid("1d924fa2-af88-4f37-bf34-4da69005531e")
                    , new Guid("d0b34a90-9d4a-42cf-89c8-5c01258d7776")
                });
            var corporateDataMock = new Mock<IRepositoryCollection<CorporateData>>();
            corporateDataMock.Setup(
                m =>
                    m.ReadWhere(x => x.ComprehensiveEsgResearchState.ResearchState == ResearchState.ResearchAvailable, null, null, null, x => x.Id, x => x.ComprehensiveEsgResearchState,
                        x => x.ControversyResearchState, x => x.CapitalIqId, x => x.CompanyName, x => x.CompanyType,
                        x => x.GICSIndustryGroup, x => x.PeerGroupShortName, x => x.TemplateType, x => x.UniverseIds))
                .Returns(corporateTestData);

            IRepositoryCollection<CompanyHistoricalDataIndicators> historicalDataRepository =
                new CompanyHistoricalDataIndicatorsRepository(_sutConnectionString);
            IReferenceUniversesRepository universeRepository =
                ReferenceUniversesRepository.GetInstance(_clientConnectionString);
            IWeightMatricesRepository weightMatrixRepository =
                WeightMatricesRepository.GetInstance(_clientConnectionString);
            IRepositoryCollection<ESGProfile> esgProfileRepository =
                new RepositoryCollection<ESGProfile>(_clientConnectionString);
            IRepositoryCollection<CompanyHistoricalPerformance> companyHistoricalPerformanceLast36MonthsRepository =
                new RepositoryCollection<CompanyHistoricalPerformance>(_sutConnectionString);
            IRepositoryCollection<IndicatorTemplate> indicatorTemplateRepository =
                new RepositoryCollection<IndicatorTemplate>(_clientConnectionString);

            _sutHistoricalDataSyncTool = new HistoricalDataSyncTool(corporateDataMock.Object, historicalDataRepository,
                universeRepository, weightMatrixRepository, esgProfileRepository,
                companyHistoricalPerformanceLast36MonthsRepository, indicatorTemplateRepository);


            var watch = new Stopwatch();

            watch.Start();
            var data = _sutHistoricalDataSyncTool.ComputeHistoryData(new DateTime(2014, 05, 02), 1);
            watch.Stop();

            Console.WriteLine(" Time Elapsed {0} ms", watch.Elapsed.TotalMilliseconds);


            Assert.True(data.Any());
        }

        [Fact]
        public void TestMethodWithTwoCompanies()
        {
            var corporateTestData = GetCorporateData(new[] { _companyId, _companyId2 });

            var testDate = new DateTime(2014, 05, 02);

            var corporateDataMock = new Mock<IRepositoryCollection<CorporateData>>();
            // ReSharper disable once RedundantBoolCompare
            corporateDataMock.Setup(m => m.ReadWhere(x => x.ComprehensiveEsgResearchState.ResearchState == ResearchState.ResearchAvailable, null, null, null, x => x.Id, x => x.ComprehensiveEsgResearchState, x => x.ControversyResearchState, x => x.CapitalIqId, x => x.CompanyName, x => x.CompanyType, x => x.GICSIndustryGroup, x => x.PeerGroupShortName, x => x.TemplateType, x => x.UniverseIds)).Returns(corporateTestData);

            IRepositoryCollection<CompanyHistoricalDataIndicators> historicalDataRepository = new RepositoryCollection<CompanyHistoricalDataIndicators>(_sutConnectionString);
            IReferenceUniversesRepository universeRepository = ReferenceUniversesRepository.GetInstance(_clientConnectionString);
            IWeightMatricesRepository weightMatrixRepository = WeightMatricesRepository.GetInstance(_clientConnectionString);
            IRepositoryCollection<ESGProfile> esgProfileRepository = new RepositoryCollection<ESGProfile>(_clientConnectionString);
            IRepositoryCollection<CompanyHistoricalPerformance> companyHistoricalPerformanceLast36MonthsRepository = new RepositoryCollection<CompanyHistoricalPerformance>(_sutConnectionString);
            IRepositoryCollection<IndicatorTemplate> indicatorTemplateRepository = new RepositoryCollection<IndicatorTemplate>(_clientConnectionString);
            _sutHistoricalDataSyncTool = new HistoricalDataSyncTool(corporateDataMock.Object, historicalDataRepository, universeRepository, weightMatrixRepository, esgProfileRepository, companyHistoricalPerformanceLast36MonthsRepository, indicatorTemplateRepository);
            var data = _sutHistoricalDataSyncTool.ComputeHistoryData(testDate, 1);

            var text = JsonConvert.SerializeObject(data, Formatting.Indented);

            File.WriteAllText(@"c:\programdata\testResult.txt", text);

            Assert.True(data.Any());
        }

        [Fact]
        public void TestMethodOneCompanyThatIsMissingInHistoricalPath()
        {
            var corporateTestData = GetCorporateData(new[] { _companyId4 });

            var testDate = new DateTime(2013, 08, 01);

            var corporateDataMock = new Mock<IRepositoryCollection<CorporateData>>();
            // ReSharper disable once RedundantBoolCompare
            corporateDataMock.Setup(m => m.ReadWhere(x => x.ComprehensiveEsgResearchState.ResearchState == ResearchState.ResearchAvailable, null, null, null, x => x.Id, x => x.ComprehensiveEsgResearchState, x => x.ControversyResearchState, x => x.CapitalIqId, x => x.CompanyName, x => x.CompanyType, x => x.GICSIndustryGroup, x => x.PeerGroupShortName, x => x.TemplateType, x => x.UniverseIds)).Returns(corporateTestData);

            IRepositoryCollection<CompanyHistoricalDataIndicators> historicalDataRepository = new RepositoryCollection<CompanyHistoricalDataIndicators>(_sutConnectionString);
            IReferenceUniversesRepository universeRepository = ReferenceUniversesRepository.GetInstance(_clientConnectionString);
            IWeightMatricesRepository weightMatrixRepository = WeightMatricesRepository.GetInstance(_clientConnectionString);
            IRepositoryCollection<ESGProfile> esgProfileRepository = new RepositoryCollection<ESGProfile>(_clientConnectionString);
            IRepositoryCollection<CompanyHistoricalPerformance> companyHistoricalPerformanceLast36MonthsRepository = new RepositoryCollection<CompanyHistoricalPerformance>(_sutConnectionString);
            IRepositoryCollection<IndicatorTemplate> indicatorTemplateRepository = new RepositoryCollection<IndicatorTemplate>(_clientConnectionString);
            _sutHistoricalDataSyncTool = new HistoricalDataSyncTool(corporateDataMock.Object, historicalDataRepository, universeRepository, weightMatrixRepository, esgProfileRepository, companyHistoricalPerformanceLast36MonthsRepository, indicatorTemplateRepository);
            var data = _sutHistoricalDataSyncTool.ComputeHistoryData(testDate, 1);

            var text = JsonConvert.SerializeObject(data, Formatting.Indented);

            File.WriteAllText(@"c:\programdata\testResult.txt", text);

            Assert.True(data.Any());
        }

        private IList<CorporateData> GetCorporateData(Guid[] guids)
        {
            var dwRep = new RepositoryCollection<CorporateData>(_clientConnectionString);
            //return dwRep.ReadbyIds(new[] {_companyId}).Values.ToList();
            return dwRep.ReadWhere(x => guids.Contains(x.Id), includeFields: new Expression<Func<CorporateData, object>>[] { x => x.Id, x => x.CapitalIqId, x => x.ComprehensiveEsgResearchState, x => x.ControversyResearchState, x => x.CompanyName, x => x.CompanyType, x => x.GICSIndustryGroup, x => x.PeerGroupShortName, x => x.TemplateType, x => x.UniverseIds });

        }



    }
}
